<!doctype html><html lang=zh-cn data-theme=dark>
<head>
<meta charset=utf-8>
<meta name=viewport content="width=device-width">
<meta name=theme-color content="#222" media="(prefers-color-scheme: dark)">
<meta name=generator content="Hugo 0.92.2">
<link rel="shortcut icon" type=image/x-icon href=/imgs/icons/favicon.ico>
<link rel=icon type=image/x-icon href=/imgs/icons/favicon.ico>
<link rel=icon type=image/png sizes=16x16 href=/imgs/icons/favicon_16x16_next.png>
<link rel=icon type=image/png sizes=32x32 href=/imgs/icons/favicon_32_32_next.png>
<link rel=apple-touch-icon sizes=180x180 href=/imgs/icons/apple_touch_icon_next.png>
<meta itemprop=name content="PHP中把数据库查询结果输出为json格式">
<meta itemprop=description content="PHP中把数据库查询结果输出为json格式">
<meta itemprop=datePublished zgotmplz>
<meta itemprop=dateModified zgotmplz>
<meta itemprop=image content="https://dzmm.gitee.io/imgs/hugo_next_avatar.png">
<meta itemprop=keywords content="php,json">
<link type=text/css rel=stylesheet href=https://cdn.staticfile.org/font-awesome/6.1.2/css/all.min.css>
<link type=text/css rel=stylesheet href=https://cdn.staticfile.org/animate.css/3.1.1/animate.min.css>
<link type=text/css rel=stylesheet href=https://cdn.staticfile.org/viewerjs/1.11.0/viewer.min.css>
<link rel=stylesheet href=/css/main.min.659f2a18a0de6aa5054906011a584c8ff887591456aea225eabf98f2cfae91b7.css>
<style type=text/css>.post-footer,.flinks-list-footer hr:after{content:"~ 我可是有底线的哟 ~"}</style>
<script type=text/javascript>(function(){localDB={set:function(b,c,a){if(a===0)return;const d=new Date,e=a*864e5,f={value:c,expiry:d.getTime()+e};localStorage.setItem(b,JSON.stringify(f))},get:function(a){const b=localStorage.getItem(a);if(!b)return void 0;const c=JSON.parse(b),d=new Date;return d.getTime()>c.expiry?(localStorage.removeItem(a),void 0):c.value}},theme={active:function(){const a=localDB.get('theme');if(a==void 0)return;theme.toggle(a),window.matchMedia("(prefers-color-scheme: dark)").addListener(function(a){theme.toggle(a.matches?'dark':'light')})},toggle:function(a){document.documentElement.setAttribute('data-theme',a),localDB.set('theme',a,2);const b=document.querySelector('iframe.giscus-frame');if(b){const c={setConfig:{theme:a}};b.contentWindow.postMessage({giscus:c},'https://giscus.app')}}},theme.active()})(window)</script>
<script class=next-config data-name=page type=application/json>{"comments":false,"isHome":false,"isPage":true,"path":"2019-02-26-html-val.html","permalink":"https://dzmm.gitee.io/post/2019-02-26-html-val.html","title":"PHP中把数据库查询结果输出为json格式","waline":{"js":[{"alias":"waline","alias_name":"@waline/client","file":"dist/pageview.js","name":"pageview","version":"2.13.0"},{"alias":"waline","alias_name":"@waline/client","file":"dist/comment.js","name":"comment","version":"2.13.0"}]}}</script>
<script type=text/javascript>document.addEventListener('DOMContentLoaded',()=>{var a=document.createElement('script');a.charset="UTF-8",a.src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js",a.async=!1,a.defer=!0,document.head.appendChild(a),a.onload=function(){NexT.utils.fmtBusuanzi()}})</script>
<title>PHP中把数据库查询结果输出为json格式 - 烂笔头</title>
<noscript>
<link rel=stylesheet href=/css/noscript.css>
</noscript>
</head>
<body itemscope itemtype=http://schema.org/WebPage class=use-motion>
<div class=headband></div>
<main class=main>
<header class=header itemscope itemtype=http://schema.org/WPHeader>
<div class=header-inner>
<div class=site-brand-container>
<div class=site-nav-toggle>
<div class=toggle aria-label role=button>
<span class=toggle-line></span>
<span class=toggle-line></span>
<span class=toggle-line></span>
</div>
</div>
<div class=site-meta>
<a href=/ class=brand rel=start>
<i class=logo-line></i>
<h1 class=site-title>烂笔头</h1>
<i class=logo-line></i>
</a>
<p class=site-subtitle itemprop=description>为 Hugo 打造的主题</p>
</div>
<div class=site-nav-right>
<div class="toggle popup-trigger">
<i class="fa fa-search fa-fw fa-lg"></i>
</div>
</div>
</div>
<nav class=site-nav>
<ul class="main-menu menu">
<li class="menu-item menu-item-home">
<a href=/ class=hvr-icon-pulse rel=section>
<i class="fa fa-home hvr-icon"></i>首页
</a>
</li>
<li class="menu-item menu-item-about">
<a href=/ class=hvr-icon-pulse rel=section>
<i class="fa fa-user hvr-icon"></i>关于
</a>
</li>
<li class="menu-item menu-item-flinks">
<a href=/ class=hvr-icon-pulse rel=section>
<i class="fa fa-thumbs-up hvr-icon"></i>站点示例
</a>
</li>
<li class="menu-item menu-item-archives">
<a href=/ class=hvr-icon-pulse rel=section>
<i class="fa fa-archive hvr-icon"></i>归档
<span class=badge>12</span>
</a>
</li>
<li class="menu-item menu-item-commonweal">
<a href=/404.html class=hvr-icon-pulse rel=section>
<i class="fa fa-heartbeat hvr-icon"></i>公益 404
</a>
</li>
<li class="menu-item menu-item-search">
<a role=button class="popup-trigger hvr-icon-pulse">
<i class="fa fa-search fa-fw hvr-icon"></i>搜索
</a>
</li>
</ul>
</nav>
<div class=search-pop-overlay>
<div class="popup search-popup">
<div class=search-header>
<span class=search-icon>
<i class="fa fa-search"></i>
</span>
<div class=search-input-container>
<input autocomplete=off autocapitalize=off maxlength=80 placeholder=搜索... spellcheck=false type=search class=search-input>
</div>
<span class=popup-btn-close role=button>
<i class="fa fa-times-circle"></i>
</span>
</div>
<div class="search-result-container no-result">
<div class=search-result-icon>
<i class="fa fa-spinner fa-pulse fa-5x"></i>
</div>
</div>
</div>
</div>
</div>
<div class="toggle sidebar-toggle" role=button>
<span class=toggle-line></span>
<span class=toggle-line></span>
<span class=toggle-line></span>
</div>
<aside class=sidebar>
<div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
<ul class=sidebar-nav>
<li class=sidebar-nav-toc>
文章目录
</li>
<li class=sidebar-nav-overview>
站点概览
</li>
</ul>
<div class=sidebar-panel-container>
<div class="post-toc-wrap sidebar-panel">
<div class="post-toc animated"><nav id=TableOfContents>
<ul>
<li>
<ul>
<li><a href=#说明>说明</a></li>
<li><a href=#例子>例子</a></li>
</ul>
</li>
</ul>
</nav></div>
</div>
<div class="site-overview-wrap sidebar-panel">
<div class="site-author site-overview-item animated" itemprop=author itemscope itemtype=http://schema.org/Person>
<img class=site-author-image itemprop=image alt="NexT 主题" src=/imgs/img-lazy-loading.gif data-src=/imgs/hugo_next_avatar.png>
<p class=site-author-name itemprop=name>NexT 主题</p>
<div class=site-description itemprop=description>保持简单的易用性和强大的功能。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
<nav class=site-state>
<div class="site-state-item site-state-posts">
<a href=/archives/>
<span class=site-state-item-count>12</span>
<span class=site-state-item-name>日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href=/categories/>
<span class=site-state-item-count>8</span>
<span class=site-state-item-name>分类</span>
</a>
</div>
<div class="site-state-item site-state-tags">
<a href=/tags/>
<span class=site-state-item-count>17</span>
<span class=site-state-item-name>标签</span>
</a>
</div>
</nav>
</div>
<div class="links-of-social site-overview-item animated">
<span class=links-of-social-item>
<a href=https://github.com/caoqing title="Github → https://github.com/caoqing" rel=noopener target=_blank>
Github
</a>
</span>
</div>
<div class="cc-license animated" itemprop=license>
<a href=https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh class=cc-opacity rel=noopener target=_blank title=共享知识>
<img src=/imgs/img-lazy-loading.gif data-src=/imgs/cc/big/by_nc_sa.svg alt=共享知识>
</a>
</div>
<div class="links-of-blogroll site-overview-item animated">
<div class=links-of-blogroll-title>
<i class="fa fa-globe fa-fw"></i>
友情链接
</div>
<ul class=links-of-blogroll-list>
<li class=links-of-blogroll-item>
<a href=https://gitee.com/hugo-next/hugo-theme-next title=https://gitee.com/hugo-next/hugo-theme-next target=_blank>Hugo-NexT</a>
</li>
<li class=links-of-blogroll-item>
<a href=https://lisenhui.cn title=https://lisenhui.cn target=_blank>凡梦星尘空间站</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div id=siteinfo-card-widget class=sidebar-card-widget>
<div class=item-headline>
<i class="fas fa-chart-line"></i>
<span>网站资讯</span>
</div>
<div class=siteinfo>
<div class=siteinfo-item>
<div class=item-name><i class="fa-solid fa-calendar-check"></i>已运行：</div>
<div class=item-count id=runTimes data-publishdate=2022-06-02T11:52:03+08:00></div>
</div>
<div class=siteinfo-item>
<div class=item-name>
<i class="fas fa fa-user"></i>总访客数：
</div>
<div class=item-count id=busuanzi_value_site_uv><i class="fa fa-sync fa-spin"></i></div>
</div>
<div class=siteinfo-item>
<div class=item-name>
<i class="fas fa fa-eye"></i>页面浏览：
</div>
<div class=item-count id=busuanzi_value_site_pv><i class="fa fa-sync fa-spin"></i></div>
</div>
<div class=siteinfo-item>
<div class=item-name><i class="fa fa-font"></i>总字数：</div>
<div class=item-count id=wordsCount data-count=12210></div>
</div>
<div class=siteinfo-item>
<div class=item-name><i class="fa fa-mug-hot"></i>阅读约：</div>
<div class=item-count id=readTimes data-times=31></div>
</div>
<div class=siteinfo-item>
<div class=item-name><i class="fa fa-clock-rotate-left"></i>最后更新于：</div>
<div class=item-count id=last-push-date data-lastpushdate=2022-12-21T00:00:00+00:00></div>
</div>
</div>
</div>
</aside>
<div class=sidebar-dimmer></div>
</header>
<div class=tool-buttons>
<div id=toggle-theme class=button title=深浅模式切换>
<i class="fas fa-adjust"></i>
</div>
<div class=back-to-top role=button title=返回顶部>
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
</div>
<div class=reading-progress-bar></div>
<a role=button class="book-mark-link book-mark-link-fixed"></a>
<a href=https://github.com/hugo-next rel="noopener external nofollow noreferrer" target=_blank title="Follow me on GitHub" class="exturl github-corner"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0 0 115 115h15l12 27L250 250V0z"/><path d="M128.3 109C113.8 99.7 119 89.6 119 89.6 122 82.7 120.5 78.6 120.5 78.6 119.2 72 123.4 76.3 123.4 76.3 127.3 80.9 125.5 87.3 125.5 87.3 122.9 97.6 130.6 101.9 134.4 103.2" fill="currentcolor" style="transform-origin:130px 106px" class="octo-arm"/><path d="M115 115C114.9 115.1 118.7 116.5 119.8 115.4l13.9-13.8C136.9 99.2 139.9 98.4 142.2 98.6 133.8 88 127.5 74.4 143.8 58 148.5 53.4 154 51.2 159.7 51 160.3 49.4 163.2 43.6 171.4 40.1 171.4 40.1 176.1 42.5 178.8 56.2 183.1 58.6 187.2 61.8 190.9 65.4 194.5 69 197.7 73.2 200.1 77.6 213.8 80.2 216.3 84.9 216.3 84.9 212.7 93.1 206.9 96 205.4 96.6 205.1 102.4 203 107.8 198.3 112.5 181.9 128.9 168.3 122.5 157.7 114.1 157.9 116.9 156.7 120.9 152.7 124.9L141 136.5C139.8 137.7 141.6 141.9 141.8 141.8z" fill="currentcolor" class="octo-body"/></svg>
</a>
<noscript>
<div class=noscript-warning>Theme NexT works best with JavaScript enabled</div>
</noscript>
<div class="main-inner post posts-expand">
<div class=post-block>
<article itemscope itemtype=http://schema.org/Article class=post-content lang>
<link itemprop=mainEntityOfPage href=https://dzmm.gitee.io/post/2019-02-26-html-val.html>
<span hidden itemprop=author itemscope itemtype=http://schema.org/Person>
<meta itemprop=image content="/imgs/hugo_next_avatar.png">
<meta itemprop=name content="NexT 主题">
</span>
<span hidden itemprop=publisher itemscope itemtype=http://schema.org/Organization>
<meta itemprop=name content="NexT 主题">
<meta itemprop=description content="保持简单的易用性和强大的功能。">
</span>
<span hidden itemprop=post itemscope itemtype=http://schema.org/CreativeWork>
<meta itemprop=name content="PHP中把数据库查询结果输出为json格式">
<meta itemprop=description content="PHP中把数据库查询结果输出为json格式">
</span>
<header class=post-header>
<h1 class=post-title itemprop="name headline">
PHP中把数据库查询结果输出为json格式
</h1>
<div class=post-meta-container>
<div class=post-meta-items>
<span class=post-meta-item>
<span class=post-meta-item-icon>
<i class="far fa-calendar"></i>
</span>
<span class=post-meta-item-text title=发表于>
发表于：
</span>
<time title="创建时间：2019-02-21 00:00:00 +0000 UTC" itemprop="dateCreated datePublished" datetime="2019-02-21 00:00:00 +0000 UTC">
2019-02-21
</time>
</span>
<span class=post-meta-item>
<span class=post-meta-item-icon>
<i class="far fa-calendar-check"></i>
</span>
<span class=post-meta-item-text title=更新于>
更新于：
</span>
<time title=修改时间：2022-09-10T21:02:32+08:00 itemprop=dateModified datetime=2022-09-10T21:02:32+08:00>2022-09-10</time>
</span>
<span class=post-meta-item>
<span class=post-meta-item-icon>
<i class="far fa-folder-open"></i>
</span>
<span class=post-meta-item-text title=分类于>
分类于：
</span>
<span itemprop=about itemscope itemtype=http://schema.org/Thing>
<a href=/categories/php itemprop=url rel=index>
<span itemprop=name>php</span>
</a>
</span>
</span>
</div>
<div class=post-meta-items>
<span class=post-meta-item title=字数>
<span class=post-meta-item-icon>
<i class="far fa-file-word"></i>
</span>
<span class=post-meta-item-text>字数：</span>
<span>1409</span>
</span>
<span class=post-meta-item title=阅读>
<span class=post-meta-item-icon>
<i class="far fa-clock"></i>
</span>
<span class=post-meta-item-text>阅读：&ap;</span>
<span>3分钟</span>
</span>
<span class=post-meta-item title=浏览>
<span class=post-meta-item-icon>
<i class="far fa-eye"></i>
</span>
<span class=post-meta-item-text>
浏览：
</span>
<span id=busuanzi_value_page_pv data-path=/post/2019-02-26-html-val.html>
<i class="fa fa-sync fa-spin"></i>
</span>
</span>
</div>
</div>
</header>
<div class="post-body autonumber" itemprop=articleBody>
<h3 id=说明>说明
<a class=header-anchor href=#%e8%af%b4%e6%98%8e></a>
</h3><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2><code class=language-php data-lang=php><span style=color:#f92672>&lt;?</span><span style=color:#a6e22e>php</span>  
<span style=color:#a6e22e>header</span>(<span style=color:#e6db74>&#34;Content-type:text/html;charset=utf-8&#34;</span>);<span style=color:#75715e>//字符编码设置  
</span><span style=color:#75715e></span>$servername <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;localhost&#34;</span>;  
$username <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;root&#34;</span>;  
$password <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;root&#34;</span>;  
$dbname <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;tjks&#34;</span>;  

<span style=color:#75715e>// 创建连接  
</span><span style=color:#75715e></span>$con <span style=color:#f92672>=</span><span style=color:#a6e22e>mysqli_connect</span>($servername, $username, $password, $dbname);  

<span style=color:#75715e>// 检测连接 
</span><span style=color:#75715e></span>$sql <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;SELECT * FROM brands&#34;</span>;  
$result <span style=color:#f92672>=</span> <span style=color:#a6e22e>mysqli_query</span>($con,$sql);  
<span style=color:#66d9ef>if</span> (<span style=color:#f92672>!</span>$result) {
    <span style=color:#a6e22e>printf</span>(<span style=color:#e6db74>&#34;Error: %s</span><span style=color:#ae81ff>\n</span><span style=color:#e6db74>&#34;</span>, <span style=color:#a6e22e>mysqli_error</span>($con));
    <span style=color:#66d9ef>exit</span>();
}

$jarr <span style=color:#f92672>=</span> <span style=color:#66d9ef>array</span>();
<span style=color:#66d9ef>while</span> ($rows<span style=color:#f92672>=</span><span style=color:#a6e22e>mysqli_fetch_array</span>($result,<span style=color:#a6e22e>MYSQL_ASSOC</span>)){
    $count<span style=color:#f92672>=</span><span style=color:#a6e22e>count</span>($rows);<span style=color:#75715e>//不能在循环语句中，由于每次删除 row数组长度都减小  
</span><span style=color:#75715e></span>    <span style=color:#66d9ef>for</span>($i<span style=color:#f92672>=</span><span style=color:#ae81ff>0</span>;$i<span style=color:#f92672>&lt;</span>$count;$i<span style=color:#f92672>++</span>){  
        <span style=color:#a6e22e>unset</span>($rows[$i]);<span style=color:#75715e>//删除冗余数据  
</span><span style=color:#75715e></span>    }
    <span style=color:#a6e22e>array_push</span>($jarr,$rows);
}
<span style=color:#a6e22e>print_r</span>($jarr);<span style=color:#75715e>//查看数组
</span><span style=color:#75715e></span><span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#34;&lt;br/&gt;&#34;</span>;
 
<span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#39;&lt;hr&gt;&#39;</span>;

<span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#39;编码后的json字符串：&#39;</span>;
<span style=color:#66d9ef>echo</span> $str<span style=color:#f92672>=</span><span style=color:#a6e22e>json_encode</span>(($jarr),<span style=color:#a6e22e>JSON_UNESCAPED_UNICODE</span>);<span style=color:#75715e>//将数组进行json编码
</span><span style=color:#75715e></span><span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#39;&lt;br&gt;&#39;</span>;
$arr<span style=color:#f92672>=</span><span style=color:#a6e22e>json_decode</span>($str);<span style=color:#75715e>//再进行json解码
</span><span style=color:#75715e></span><span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#39;解码后的数组：&#39;</span>;
<span style=color:#a6e22e>print_r</span>($arr);<span style=color:#75715e>//打印解码后的数组，数据存储在对象数组中
</span><span style=color:#75715e></span><span style=color:#a6e22e>mysqli_close</span>($con);
<span style=color:#75715e>?&gt;</span><span style=color:#960050;background-color:#1e0010>
</span></code></pre></div><p>输出内容如下：</p>
<div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2><code class=language-php data-lang=php><span style=color:#66d9ef>Array</span> ( [<span style=color:#a6e22e>id</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#ae81ff>1</span> [<span style=color:#a6e22e>name</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#a6e22e>GNC</span> ) <span style=color:#66d9ef>Array</span> ( [<span style=color:#a6e22e>id</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#ae81ff>2</span> [<span style=color:#a6e22e>name</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#a6e22e>TCBJ</span> ) <span style=color:#66d9ef>Array</span> ( [<span style=color:#a6e22e>id</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#ae81ff>3</span> [<span style=color:#a6e22e>name</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#a6e22e>TJKS</span> )
<span style=color:#a6e22e>Unicode编码后的json数据</span><span style=color:#f92672>:</span> [{<span style=color:#e6db74>&#34;id&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;1&#34;</span>,<span style=color:#e6db74>&#34;name&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;GNC&#34;</span>},{<span style=color:#e6db74>&#34;id&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;2&#34;</span>,<span style=color:#e6db74>&#34;name&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;TCBJ&#34;</span>},{<span style=color:#e6db74>&#34;id&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;3&#34;</span>,<span style=color:#e6db74>&#34;name&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;TJKS&#34;</span>}] 
</code></pre></div><p>可以看到，直接进行json_encode();编码，是将三个数组分别转为了json格式，而且两端会出现中括号！！还有另外一种写法</p>
<div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2><code class=language-php data-lang=php><span style=color:#f92672>&lt;?</span><span style=color:#a6e22e>php</span>  
<span style=color:#a6e22e>header</span>(<span style=color:#e6db74>&#34;Content-type:text/html;charset=utf-8&#34;</span>);<span style=color:#75715e>//字符编码设置  
</span><span style=color:#75715e></span>$servername <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;localhost&#34;</span>;  
$username <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;root&#34;</span>;  
$password <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;root&#34;</span>;  
$dbname <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;tjks&#34;</span>;  

<span style=color:#75715e>// 创建连接  
</span><span style=color:#75715e></span>$con <span style=color:#f92672>=</span><span style=color:#a6e22e>mysqli_connect</span>($servername, $username, $password, $dbname);  

<span style=color:#75715e>// 检测连接  
</span><span style=color:#75715e></span>
  
$sql <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;SELECT * FROM brands&#34;</span>;  
$result <span style=color:#f92672>=</span> <span style=color:#a6e22e>mysqli_query</span>($con,$sql);  
<span style=color:#66d9ef>if</span> (<span style=color:#f92672>!</span>$result) {
    <span style=color:#a6e22e>printf</span>(<span style=color:#e6db74>&#34;Error: %s</span><span style=color:#ae81ff>\n</span><span style=color:#e6db74>&#34;</span>, <span style=color:#a6e22e>mysqli_error</span>($con));
    <span style=color:#66d9ef>exit</span>();
}

$jarr <span style=color:#f92672>=</span> <span style=color:#66d9ef>array</span>();
<span style=color:#66d9ef>while</span> ($rows<span style=color:#f92672>=</span><span style=color:#a6e22e>mysqli_fetch_array</span>($result,<span style=color:#a6e22e>MYSQL_ASSOC</span>)){
    $count<span style=color:#f92672>=</span><span style=color:#a6e22e>count</span>($rows);<span style=color:#75715e>//不能在循环语句中，由于每次删除 row数组长度都减小  
</span><span style=color:#75715e></span>    <span style=color:#66d9ef>for</span>($i<span style=color:#f92672>=</span><span style=color:#ae81ff>0</span>;$i<span style=color:#f92672>&lt;</span>$count;$i<span style=color:#f92672>++</span>){  
        <span style=color:#a6e22e>unset</span>($rows[$i]);<span style=color:#75715e>//删除冗余数据  
</span><span style=color:#75715e></span>    }
    <span style=color:#a6e22e>array_push</span>($jarr,$rows);
}
<span style=color:#a6e22e>print_r</span>($jarr);<span style=color:#75715e>//查看数组
</span><span style=color:#75715e></span><span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#34;&lt;br/&gt;&#34;</span>;
<span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#39;&lt;hr&gt;&#39;</span>;

$jobj<span style=color:#f92672>=</span><span style=color:#66d9ef>new</span> <span style=color:#66d9ef>stdclass</span>();<span style=color:#75715e>//实例化stdclass，这是php内置的空类，可以用来传递数据，由于json_encode后的数据是以对象数组的形式存放的，
</span><span style=color:#75715e>//所以我们生成的时候也要把数据存储在对象中
</span><span style=color:#75715e></span><span style=color:#66d9ef>foreach</span>($jarr <span style=color:#66d9ef>as</span> $key<span style=color:#f92672>=&gt;</span>$value){
$jobj<span style=color:#f92672>-&gt;</span>$key<span style=color:#f92672>=</span>$value;
}
<span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#39;传递属性后的对象：&#39;</span>;
<span style=color:#a6e22e>print_r</span>($jobj);<span style=color:#75715e>//打印传递属性后的对象
</span><span style=color:#75715e></span><span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#39;&lt;br&gt;&#39;</span>;
<span style=color:#66d9ef>echo</span> <span style=color:#e6db74>&#39;编码后的json字符串：&#39;</span><span style=color:#f92672>.</span><span style=color:#a6e22e>json_encode</span>(($jobj),<span style=color:#a6e22e>JSON_UNESCAPED_UNICODE</span>);<span style=color:#f92672>.</span><span style=color:#e6db74>&#39;&lt;br&gt;&#39;</span>;<span style=color:#75715e>//打印编码后的json字符串
</span><span style=color:#75715e></span><span style=color:#a6e22e>mysqli_close</span>($con);
<span style=color:#75715e>?&gt;</span><span style=color:#960050;background-color:#1e0010>
</span></code></pre></div><p>输出内容如下：</p>
<div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2><code class=language-php data-lang=php><span style=color:#66d9ef>Array</span> ( [<span style=color:#ae81ff>0</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#66d9ef>Array</span> ( [<span style=color:#a6e22e>id</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#ae81ff>1</span> [<span style=color:#a6e22e>name</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#a6e22e>GNC</span> ) [<span style=color:#ae81ff>1</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#66d9ef>Array</span> ( [<span style=color:#a6e22e>id</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#ae81ff>2</span> [<span style=color:#a6e22e>name</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#a6e22e>TCBJ</span> ) [<span style=color:#ae81ff>2</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#66d9ef>Array</span> ( [<span style=color:#a6e22e>id</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#ae81ff>3</span> [<span style=color:#a6e22e>name</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#a6e22e>TJKS</span> ) )
<span style=color:#a6e22e>传递属性后的对象：stdClass</span> <span style=color:#a6e22e>Object</span> ( [<span style=color:#ae81ff>0</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#66d9ef>Array</span> ( [<span style=color:#a6e22e>id</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#ae81ff>1</span> [<span style=color:#a6e22e>name</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#a6e22e>GNC</span> ) [<span style=color:#ae81ff>1</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#66d9ef>Array</span> ( [<span style=color:#a6e22e>id</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#ae81ff>2</span> [<span style=color:#a6e22e>name</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#a6e22e>TCBJ</span> ) [<span style=color:#ae81ff>2</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#66d9ef>Array</span> ( [<span style=color:#a6e22e>id</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#ae81ff>3</span> [<span style=color:#a6e22e>name</span>] <span style=color:#f92672>=&gt;</span> <span style=color:#a6e22e>TJKS</span> ) )
<span style=color:#a6e22e>编码后的json字符串：</span>{<span style=color:#e6db74>&#34;0&#34;</span><span style=color:#f92672>:</span>{<span style=color:#e6db74>&#34;id&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;1&#34;</span>,<span style=color:#e6db74>&#34;name&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;GNC&#34;</span>},<span style=color:#e6db74>&#34;1&#34;</span><span style=color:#f92672>:</span>{<span style=color:#e6db74>&#34;id&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;2&#34;</span>,<span style=color:#e6db74>&#34;name&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;TCBJ&#34;</span>},<span style=color:#e6db74>&#34;2&#34;</span><span style=color:#f92672>:</span>{<span style=color:#e6db74>&#34;id&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;3&#34;</span>,<span style=color:#e6db74>&#34;name&#34;</span><span style=color:#f92672>:</span><span style=color:#e6db74>&#34;TJKS&#34;</span>}}
</code></pre></div><p>这样一来，中括号没有了，数组也成为了有序的数组！</p>
<h3 id=例子>例子
<a class=header-anchor href=#%e4%be%8b%e5%ad%90></a>
</h3><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2><code class=language-php data-lang=php><span style=color:#f92672>&lt;?</span><span style=color:#a6e22e>php</span>
<span style=color:#66d9ef>require_once</span>(<span style=color:#e6db74>&#39;../ini_config.php&#39;</span>);
<span style=color:#66d9ef>require_once</span>($www_ROOT<span style=color:#f92672>.</span><span style=color:#e6db74>&#39;/conn.php&#39;</span>);

<span style=color:#a6e22e>session_start</span>();
<span style=color:#66d9ef>if</span>(<span style=color:#f92672>!</span><span style=color:#a6e22e>isset</span>($_SESSION[<span style=color:#e6db74>&#39;banji&#39;</span>])){
    <span style=color:#66d9ef>echo</span>(<span style=color:#e6db74>&#34;非法操作！&#34;</span>);<span style=color:#66d9ef>exit</span>;
  }

$banji<span style=color:#f92672>=</span>$_SESSION[<span style=color:#e6db74>&#39;banji&#39;</span>];
$bzrname<span style=color:#f92672>=</span>$_SESSION[<span style=color:#e6db74>&#39;bzrname&#39;</span>];
<span style=color:#75715e>//session_destroy();
</span><span style=color:#75715e></span>$sql <span style=color:#f92672>=</span> <span style=color:#e6db74>&#34;select * from stu where banji=&#34;</span><span style=color:#f92672>.</span><span style=color:#e6db74>&#34;&#39;</span><span style=color:#e6db74>$banji</span><span style=color:#e6db74>&#39;&#34;</span><span style=color:#f92672>.</span><span style=color:#e6db74>&#34;order by convert(banji using gbk) ASC,id ASC&#34;</span>;
$ret <span style=color:#f92672>=</span> <span style=color:#a6e22e>mysqli_query</span>( $conn, $sql );
$infos<span style=color:#f92672>=</span><span style=color:#66d9ef>array</span>();
<span style=color:#66d9ef>while</span> ($row<span style=color:#f92672>=</span><span style=color:#a6e22e>mysqli_fetch_assoc</span>($ret)) {
    <span style=color:#a6e22e>array_push</span>($infos,$row);
}
<span style=color:#75715e>//var_dump($infos);exit;
</span><span style=color:#75715e></span><span style=color:#66d9ef>echo</span> <span style=color:#a6e22e>json_encode</span>(<span style=color:#66d9ef>array</span>(
    <span style=color:#e6db74>&#34;bzrname&#34;</span><span style=color:#f92672>=&gt;</span>$bzrname,
    <span style=color:#e6db74>&#34;banji&#34;</span><span style=color:#f92672>=&gt;</span>$banji,
    <span style=color:#e6db74>&#34;data&#34;</span> <span style=color:#f92672>=&gt;</span>$infos),<span style=color:#a6e22e>JSON_UNESCAPED_UNICODE</span>);
<span style=color:#75715e>/*echo json_encode($infos),JSON_UNESCAPED_UNICODE);    bootstraptable*/</span>
<span style=color:#75715e>//var_dump($infos);exit
</span><span style=color:#75715e></span><span style=color:#75715e>?&gt;</span><span style=color:#960050;background-color:#1e0010>
</span></code></pre></div>
</div>
<footer class=post-footer>
<div class=post-tags>
<a href=/tags/php>
php
</a>
<a href=/tags/json>
json
</a>
</div>
<div class=addthis_inline_share_toolbox style=text-align:center></div>
<hr>
<div class=post-nav>
<div class="post-nav-next post-nav-item">
<a href=/post/2019-02-25-html-php.html rel=next title=html,php操作记录>
<i class="fa fa-chevron-left"></i> html,php操作记录
</a>
</div>
<div class="post-nav-prev post-nav-item">
<a href=/post/2019-04-23-php-backup.html rel=prev title=php简单备份与还原MySql的方法>
php简单备份与还原MySql的方法
<i class="fa fa-chevron-right"></i>
</a>
</div>
</div>
</footer>
</article>
</div>
</div>
</main>
<footer class=footer>
<div class=footer-inner>
<div class=copyright>
&copy;
<span itemprop=copyrightYear>
2010 - 2023
</span>
<span class=with-love>
<i class="fa fa-heart"></i>
</span>
<span class=author itemprop=copyrightHolder>NexT 主题</span>
</div>
<div class=powered-by>
由 <a href=https://gohugo.io title=0.92.2 target=_blank>Hugo</a> & <a href=https://github.com/hugo-next/hugo-theme-next title=4.5.3 target=_blank>Hugo NexT.Gemini</a> 强力驱动
</div>
</div>
</footer>
<script type=text/javascript src=https://cdn.staticfile.org/animejs/3.2.1/anime.min.js defer></script>
<script type=text/javascript src=https://cdn.staticfile.org/viewerjs/1.11.0/viewer.min.js defer></script>
<script class=next-config data-name=main type=application/json>{"bookmark":{"color":"#222","enable":true,"save":"manual"},"copybtn":true,"darkmode":true,"giscus":{"cfg":{"category":"Comments","categoryid":null,"emit":false,"inputposition":"top","mapping":"title","reactions":false,"repo":"username/repo-name","repoid":null,"theme":"preferred_color_scheme"},"js":"https://giscus.app/client.js"},"hostname":"https://dzmm.gitee.io","i18n":{"ds_day":" 天前","ds_days":" 天 ","ds_hour":" 小时前","ds_hours":" 小时 ","ds_just":"刚刚","ds_min":" 分钟前","ds_mins":" 分钟","ds_month":" 个月前","ds_years":" 年 ","empty":"没有找到任何搜索结果：${query}","hits":"找到 ${hits} 个搜索结果","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","placeholder":"搜索..."},"lang":"zh-CN","lazyload":false,"localSearch":{"enable":true,"limit":1e3,"path":"/searchindexes.xml","preload":false,"topnperarticle":-1,"trigger":"auto","unescape":false},"motion":{"async":true,"enable":true,"transition":{"collheader":"fadeInLeft","postblock":"fadeIn","postbody":"fadeInDown","postheader":"fadeInDown","sidebar":"fadeInUp"}},"postmeta":{"comments":{"enable":false,"plugin":"waline"},"views":{"enable":true,"plugin":"busuanzi"}},"root":"/","scheme":"Gemini","sidebar":{"display":"post","offset":12,"padding":18,"position":"left","width":256},"vendor":{"plugins":"qiniu","router":"https://cdn.staticfile.org"},"version":"4.5.3","waline":{"cfg":{"emoji":false,"imguploader":false,"placeholder":"请文明发言哟 ヾ(≧▽≦*)o","reaction":true,"reactiontext":["点赞","踩一下","得意","不屑","尴尬","睡觉"],"reactiontitle":"你认为这篇文章怎么样？","requiredmeta":["nick","mail"],"serverurl":null,"sofa":"快来发表你的意见吧 (≧∀≦)ゞ","wordlimit":200},"css":{"alias":"waline","file":"dist/waline.css","name":"@waline/client","version":"2.13.0"},"js":{"alias":"waline","file":"dist/waline.js","name":"@waline/client","version":"2.13.0"}}}</script>
<script type=text/javascript src=/js/main.min.da1a84618d05ef38d3aaca7a8e93f6433f9bb147d8ffcc3a840de8a03dc5ff9f.js defer></script>
</body>
</html>